/*
 * CadastrodeEstacao.java
 *
 * Created on 26 de Novembro de 2008, 16:22
 */

package br.faimg.pomar.visual;

import br.faimg.pomar.controle.BusinessDelegate.UsuarioBusinessDelegate;
import br.faimg.pomar.controle.componentevisual.ComboGrupoUsuario;
import br.faimg.pomar.controle.componentevisual.ItemTipo;
import javax.swing.JComboBox;

/**
Esta sendo utilizado o metodo getText para pegar a senha do usuario
 *
 * @author  diogo
 */
public class CadastrodeUsuarioInclusao extends javax.swing.JDialog {

    /** Creates new form CadastrodeEstacao */
    public CadastrodeUsuarioInclusao() {
        initComponents();  
                
        updateTable();
        
        incluirItensCombo();

    }
    

    public CadastrodeUsuarioInclusao(String nome, String email, String login, String tipoGrupo){ 
        initComponents();
        incluirItensCombo();
        
        okButton.setText("Alterar");
        cancelarButton.setText("Limpar");
        nomeTextField.setText(nome);
        loginTextField.setText(login);
        nomeTextField.setEnabled(false);
        loginTextField.setEnabled(false);
        jLabel1.setEnabled(false);
        jLabel6.setEnabled(false);
        incluirItensCombo();
        emailTextField.setText(email);
        senhaTextField.setEnabled(true);
        
        
        
              //Seleciona o item que foi clicado na tabela.
        for (int i = 0; i < grupoUsuarioCombo.getItemCount(); i++) {

            if (tipoGrupo.equals(((ItemTipo) grupoUsuarioCombo.getItemAt(i)).getCodigo())) {
                grupoUsuarioCombo.setSelectedIndex(i);
                break;
            }
            
            
        }
       
    }
    
    private void updateTable() {
        //Lista os itens cadastrados no banco na tablela
        //tabelaCadastrodeUsuario.setModel(new JTable(
               // new TableGrupodeUsuario().getTableModel()).getModel());
        
        //remove a coluna do codigo do produto
        //tabelaCadastrodeUsuario.removeColumn(tabelaCadastrodeUsuario.getColumn("GrupoCod"));
    }

       private void incluirItensCombo() {
        //Inlui os itens no combobox do tipo de estacao
        ComboGrupoUsuario itens = new ComboGrupoUsuario();
        JComboBox temp = new JComboBox(itens.getList());
        grupoUsuarioCombo.setModel(temp.getModel());
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        nomeTextField = new javax.swing.JTextField();
        cancelarButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        emailTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        loginTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        grupoUsuarioCombo = new javax.swing.JComboBox();
        senhaTextField = new javax.swing.JPasswordField();
        confirmarsenhaTextField = new javax.swing.JPasswordField();
        jSeparator1 = new javax.swing.JSeparator();
        erroSenha = new javax.swing.JLabel();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setTitle("Cadastro de Usuários");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Nome:");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        jLabel2.setText("e-mail: ");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, -1, -1));

        jLabel3.setText("Confirmar senha:");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 100, -1, -1));

        nomeTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nomeTextFieldActionPerformed(evt);
            }
        });
        nomeTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nomeTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(nomeTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 20, 550, -1));

        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarButtonActionPerformed(evt);
            }
        });
        jPanel1.add(cancelarButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 220, 90, -1));

        okButton.setText("Incluir");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        jPanel1.add(okButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 220, 90, -1));

        emailTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emailTextFieldActionPerformed(evt);
            }
        });
        emailTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                emailTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(emailTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 60, 550, -1));

        jLabel4.setText("Grupo:");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, -1, -1));

        jLabel5.setText("Senha:");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, -1, -1));

        loginTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginTextFieldActionPerformed(evt);
            }
        });
        loginTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                loginTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(loginTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 140, 200, -1));

        jLabel6.setText("Login:");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 140, -1, -1));

        grupoUsuarioCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        grupoUsuarioCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                grupoUsuarioComboActionPerformed(evt);
            }
        });
        jPanel1.add(grupoUsuarioCombo, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 180, 550, 20));

        senhaTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                senhaTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(senhaTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 100, 200, -1));

        confirmarsenhaTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                confirmarsenhaTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(confirmarsenhaTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 100, 200, -1));
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 210, 600, 10));

        erroSenha.setForeground(new java.awt.Color(255, 0, 0));
        erroSenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                erroSenhaKeyPressed(evt);
            }
        });
        jPanel1.add(erroSenha, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 140, 120, 10));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 640, 270));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void nomeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nomeTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_nomeTextFieldActionPerformed

private void cancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarButtonActionPerformed
    erroSenha.setText("");
    
    if ( nomeTextField.getText().equals("") && confirmarsenhaTextField.getText().equals("")&&
         emailTextField.getText().equals("") && loginTextField.getText().equals("")){
       
        this.setVisible(false); 
        
    } else {
        
        if(confirmarsenhaTextField.getText().equals("")&&
                emailTextField.getText().equals("") ){
                
                this.setVisible(false);
                
        }
        
        if(okButton.getText().equals("Alterar")){
            nomeTextField.setEditable(false);
            loginTextField.setEditable(false);
            senhaTextField.setText("");
            loginTextField.setText("");
            emailTextField.setText("");    
            cancelarButton.setText("Cancelar");
            nomeTextField.setEnabled(true);
            okButton.setEnabled(true);

        } else{
            nomeTextField.setText("");
            emailTextField.setText("");
            loginTextField.setText("");
            senhaTextField.setText("");
            confirmarsenhaTextField.setText("");
            cancelarButton.setText("Cancelar");
            nomeTextField.setEnabled(true);
            okButton.setEnabled(true);
        }

        
    }
}//GEN-LAST:event_cancelarButtonActionPerformed

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed

  
   UsuarioBusinessDelegate delegate = new UsuarioBusinessDelegate();
   if(nomeTextField.getText().equals("") ||
      senhaTextField.getText().equals("")||
      loginTextField.getText().equals("")){
       
      nomeTextField.setText("");
      senhaTextField.setText("");
      emailTextField.setText("");
      confirmarsenhaTextField.setText("");
      loginTextField.setText("");
      erroSenha.setText("Cadastro invalido");
   }else{
       

       erroSenha.setText("");

       if(senhaTextField.getText().equals(confirmarsenhaTextField.getText())){
         delegate.create( nomeTextField.getText(), 
                        emailTextField.getText(),
                        loginTextField.getText(), 
                        senhaTextField.getText(),
                        ((ItemTipo) grupoUsuarioCombo.getSelectedItem()).getCodigo()
                        );

        this.setVisible(false);
        //Limpa os combos apos a insercao para permitir nova insercao*/
        nomeTextField.setText("");
        emailTextField.setText("");
        loginTextField.setText("");
        senhaTextField.setText("");
        confirmarsenhaTextField.setText("");


        updateTable();
       
        
        } else{
            erroSenha.setText("Senha incorreta!");
   
        }
   }
    
}//GEN-LAST:event_okButtonActionPerformed


private void nomeTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nomeTextFieldKeyPressed
    cancelarButton.setText("Limpar");
    erroSenha.setText("");
}//GEN-LAST:event_nomeTextFieldKeyPressed

private void emailTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emailTextFieldActionPerformed
// TODO add your handling code here:
    erroSenha.setText("");
}//GEN-LAST:event_emailTextFieldActionPerformed

private void emailTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_emailTextFieldKeyPressed
    cancelarButton.setText("Limpar");
    erroSenha.setText("");
}//GEN-LAST:event_emailTextFieldKeyPressed

private void loginTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginTextFieldActionPerformed
    cancelarButton.setText("Limpar");
    erroSenha.setText("");//GEN-LAST:event_loginTextFieldActionPerformed
}                                              

private void loginTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_loginTextFieldKeyPressed
// TODO add your handling code here:
    cancelarButton.setText("Limpar");
    erroSenha.setText("");//GEN-LAST:event_loginTextFieldKeyPressed
}                                         

private void grupoUsuarioComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_grupoUsuarioComboActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_grupoUsuarioComboActionPerformed

private void erroSenhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_erroSenhaKeyPressed
// TODO add your handling code here:
}//GEN-LAST:event_erroSenhaKeyPressed

private void senhaTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_senhaTextFieldKeyPressed
// TODO add your handling code here:
    erroSenha.setText("");
}//GEN-LAST:event_senhaTextFieldKeyPressed

private void confirmarsenhaTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_confirmarsenhaTextFieldKeyPressed
    cancelarButton.setText("Limpar");
    erroSenha.setText("");//GEN-LAST:event_confirmarsenhaTextFieldKeyPressed
}


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelarButton;
    private javax.swing.JPasswordField confirmarsenhaTextField;
    private javax.swing.JTextField emailTextField;
    private javax.swing.JLabel erroSenha;
    private javax.swing.JComboBox grupoUsuarioCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField loginTextField;
    private javax.swing.JTextField nomeTextField;
    private javax.swing.JButton okButton;
    private javax.swing.JPasswordField senhaTextField;
    // End of variables declaration//GEN-END:variables

}
